'use strict';

const config = require('../config');
const morgan = require('koa-morgan');
const bodyparser = require('koa-bodyparser');

const router = require('../router');
const format = config.app.isProd ? 'combined' : 'dev';
const options = {
  stream: {
    write(str) {
      console.log(str);
    }
  }
};

module.exports = function (app) {
  app.use(morgan.middleware(format, options));
  app.use(bodyparser());
  app.use(router.routes());

  app.on('error', (err, ctx) => {
    console.error('server error:', err);
  });
};
